<?php
class Resources {
	var $htmlString;
	
	function getResourceFilesByCategory($catID) {
		global $dbname, $db, $db_selected;
		$getData = new getData();
				
		$sql = "SELECT 
					F.FID,F.CatID,F.FileTitle,F.FileDesc,F.FileName,F.FileSize,F.FileType,F.DateUpdate,
					C.DirectoryName
				FROM cdc_Files F
				LEFT JOIN cdc_sysCategory C ON F.CatID = C.CatID
				WHERE 
					F.CatID = '$catID' AND
					F.Deleted = 0";
		$result = mysql_db_query($dbname,$sql);
		
		$a=0;
		$returnArray = array();
		while ($value = mysql_fetch_assoc($result)) {
			$value['ConvFileSize'] = convertFileSize($value['FileSize']);
			$value['MimeIconClass']= setMimeIconClass($value['FileType']);
			$catDirArray = $getData->getDirFromCategory($catID);
			$catDirArray = $getData->returnData();
			
			//$value['FilePath']     = SITE_URL.'/public_files/resource_files/'.$value['DirectoryName'].'/'.$value['FileName'];
			$value['FilePath'] = SITE_URL.'/public_files/resource_files/'.$catDirArray['CatPath'].'/'.$value['FileName'];
			
			$fileDate = new Zend_Date($value['DateUpdate'], Zend_Date::ISO_8601, 'en_US');
			$newFileDate = $fileDate->toString('MMM d');
			$value['FileDateFormat'] = $newFileDate;
			$returnArray[$a] = $value;
			$a++;
		}	
		return $returnArray;
	}
	
	function getFileCategories($siteArea,$mainCat=null) {
		global $dbname, $db, $db_selected;
		
		$siteArea = urldecode($siteArea);
		
		if ($mainCat>0) {
			$sqlSubCat = ' AND C.ParentID = '.$mainCat.' ';
			$catType = 'SubCat';
			$mainID  = $mainCat;
		} else { 
			$sqlSubCat = ' AND C.ParentID = 0';
			$catType = 'MainCat';
			$mainID  = 0;
		}
		
		$sql = "SELECT C.CatID,C.CatTitle,COUNT(FID) AS NoOfFiles
			FROM cdc_sysCategory C
			LEFT JOIN cdc_Files F ON F.CatID = C.CatID 
			WHERE 
				(C.SiteArea = '$siteArea' AND 
				C.Active = 1)
				$sqlSubCat				
			GROUP BY C.CatID
			ORDER BY C.CatTitle";
		$result = mysql_db_query($dbname,$sql);	
		
		while ($value = mysql_fetch_assoc($result)) {
			$catID     = $value['CatID'];
			$catName   = $value['CatTitle'];
			$noOfFiles = $this->getFileCount($catID);
			$filesArray = $this->getResourceFilesByCategory($catID);
			
			if ($noOfFiles == 1) {
				$fileText = 'file';
			} else {
				$fileText = 'files';
			}
			
			$this->htmlString .= '<div class="emphBox"><div class="emphBoxInner" style="font-size: 1.2em;" onMouseOver="this.className = \'emphBoxHover\'" onMouseOut="this.className = \'emphBoxInner\'">';			
			$this->htmlString .= '<div onClick="togglePanel(\'fileList'.$catID.'\'); return false;" title="Click to view files" style="cursor: pointer; float: left;">';
			$this->htmlString .= '<span class="arrowDown" id="mainCatHeading'.$catID.'" style="margin-bottom: 0px; font-weight: bold;">'.$catName.'</span> <span style="font-weight: bold;" id="noOfFiles'.$catID.'">['.$noOfFiles.' '.$fileText.']</span>';
			$this->htmlString .= '</div><div style="float: right;">';
			$this->htmlString .= '</div><div style="clear: both;"></div></div></div>';
			$this->htmlString .= '<div id="fileList'.$catID.'" style="display: none;"><div id="scriptaculousNeedsMe">';
			
			if ($noOfFiles>0) {			
				$this->htmlString .= '
									<table class="dataTable" style="font-size: 1em;">
									<tr>
									<th style="width: 30%;">File Title</th>
									<th style="width: 50%;">Description</th>
									<th style="width: 10%;">Date</th>
									<th style="width: 10%;">Size</th>
									</tr>';
				for($a=0;$a<=(count($filesArray)-1);$a++) {
					if (($a%2) > 0) { 
						$rowClass = 'altRow'; 
					} else { 
						$rowClass = '';
					}
					
					$fileID       = $filesArray[$a]['FID'];
					$fileTitle    = $filesArray[$a]['FileTitle'];
					$fileDesc     = $filesArray[$a]['FileDesc'];
					$fileSize     = $filesArray[$a]['FileSize'];
					$filePath     = $filesArray[$a]['FilePath'];
					$fileName     = $filesArray[$a]['FileName'];
					$mimeIconClass= $filesArray[$a]['MimeIconClass'];
					$convFileSize = $filesArray[$a]['ConvFileSize'];
					$dateAdded    = $filesArray[$a]['DateUpdate'];
					$dateFormat   = $filesArray[$a]['FileDateFormat'];
				
					$this->htmlString .= '
									<tr id="fileRow'.$fileID.'" class="'.$rowClass.'">
									<td><div id="fileTitleContainer'.$fileID.'" class="'.$mimeIconClass.'"><a href="'.$filePath.'" title="'.$fileName.' '.$convFileSize.'">'.$fileTitle.'</a></div></td>
									<td>'.$fileDesc.'</td>
									<td>'.$dateFormat.'</td>
									<td style="text-align: right;">'.$convFileSize.'</td>
									</tr>';						
				}
				$this->htmlString .= '</table>';
			} else {
				$this->htmlString .= '<p>There are no files in this category yet.</p>';	
			}			
			
			$this->htmlString .= '</div></div><div style="height: 10px;"></div>';
		}	
	}
	
	function getFileCount($catID) {
		global $dbname, $db, $db_selected;
		
		/*
		 * Is $catID a main-cat or sub-cat?
		 */
		$sql = "SELECT ParentID from cdc_sysCategory WHERE CatID = '$catID'";
		$result = mysql_db_query($dbname,$sql);
		$row = mysql_fetch_assoc($result);
		if ($row['ParentID']>0) {
			/*
			 * $catID is a sub-category, get number of files for this sub-category
			 */
			$sql2 = "SELECT COUNT(FID) AS NoOfFiles from cdc_Files 
						WHERE CatID = '$catID' AND
						Deleted = 0";
			$result2 = mysql_db_query($dbname,$sql2);
			$row2 = mysql_fetch_assoc($result2);
			$noOfFiles = $row2['NoOfFiles'];
		} else {
			/*
			 * $catID is a main category, check for any sub-categories
			 */
			$sql2 = "SELECT CatID from cdc_sysCategory WHERE ParentID = '$catID'";
			$result2 = mysql_db_query($dbname,$sql2);
			if (mysql_num_rows($result2)>0) {
				$noOfFiles = 0;
				while ($value = mysql_fetch_assoc($result2)) {
					$newCatID = $value['CatID'];
					
					$sql3 = "SELECT COUNT(FID) AS NoOfFiles from cdc_Files 
								WHERE CatID = '$newCatID' AND
								Deleted = 0";
					$result3 = mysql_db_query($dbname,$sql3);
					$row3 = mysql_fetch_assoc($result3);
					$noOfFiles = $noOfFiles+$row3['NoOfFiles'];				
				}
			} else {
				/*
				 * There are no sub-categories, just get files for this category
				 */
				$sql3 = "SELECT COUNT(FID) AS NoOfFiles from cdc_Files 
							WHERE CatID = '$catID'  AND
							Deleted = 0";
				$result3 = mysql_db_query($dbname,$sql3);
				$row3 = mysql_fetch_assoc($result3);
				$noOfFiles = $row3['NoOfFiles'];
			}
		}
		return $noOfFiles;
	}
	
	function renderHTML() {
		return $this->htmlString;
	}
}
?>